#include "httpd.h"

int main(int c, char** v)
{
    serve_forever("12345");
    return 0;
}

void route()
{
    ROUTE_START()

    ROUTE_GET("/")
    {
        printf("HTTP/1.1 200 OK\r\n\r\n");
        printf("Content-Type: application/json\n");
        printf("Hello! You are using : %s\n", request_header("User-Agent"));
        printf("Query string :  %s\n", query_string);

        // parse the query string
        link_list_t *query_params = url_params_parse(query_string);
        if (query_params != NULL && query_params->size > 0) {
            printf("++++++++++++++++++ Traversing the query params ++++++++++++++++++\n");
            link_list_node_t * node;
            param_t * param;
            node = query_params->head->next;
            while (NULL != node->next) {
                param = (param_t *) node->value;
                printf("[%s] : %s\n", param->key, param->value);
                node = node->next;
            }
            // free memory of post data
            link_list_destroy(post_data, free_link_node_value);
        }
    }

    ROUTE_POST("/")
    {
        printf("HTTP/1.1 200 OK\r\n\r\n");
        printf("POST request, you have posted %d bytes. \n", post_data_length);
        printf("++++++++++++++++++++ The data you had posted: +++++++++++++++++++++++\n");

        printf("[POST DATA LENGTH] : %d. \n", post_data->size);
        printf("++++++++++++++++++ Traversing the post data ++++++++++++++++++\n");
        link_list_node_t * node;
        param_t * param;
        node = post_data->head->next;
        while (NULL != node->next) {
            param = (param_t *) node->value;
            printf("[key] : %s, [value] : %s\n", param->key, param->value);
            node = node->next;
        }
        // free memory of post data
        link_list_destroy(post_data, free_link_node_value);

    }

    ROUTE_GET("/user/list")
    {
        printf("HTTP/1.1 200 OK\r\n\r\n");
        printf("Content-Type: application/json\n");
        printf("Hello! You are using : \n %s", request_header("User-Agent"));

    }
  
    ROUTE_END()
}